﻿using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.Infrastructures.Interfaces;
using MediatR;
using RBAC.Write.Domain.DTOs;

namespace RBAC.Write.API.Applications.PermissionsFiles
{
    public class UserListHander:IRequestHandler<UserListCommand, ResultsDTO<UserListDTO>>
    {
        private readonly IBateRepository<UserListDTO> bate;

        public UserListHander(IBateRepository<UserListDTO> bate)
        {
            this.bate = bate;
        }

        public async Task<ResultsDTO<UserListDTO>> Handle(UserListCommand request, CancellationToken cancellationToken)
        {
            try
            {
                
                UserListDTO userListDTO = new UserListDTO() ;
                ResultsDTO<UserListDTO> res = new ResultsDTO<UserListDTO>();
                DapperHelper dapper = new DapperHelper();
                string sql = "select * from User";
                if (!string.IsNullOrEmpty(request.UserName))
                {
                    sql += $" where UserName like '%{request.UserName}%'";
                }
                res.Data = bate.GetAll(sql, userListDTO, dapper.RBACWrite).ToList();
                foreach (var item in res.Data)
                {
                    item.State = item.State == "0" ? "启用" : "禁用";
                }
                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
